<script setup>
import {getArticleCate, delArticleCate} from '@/api/article.js'
import {ref} from 'vue'
import {
  Edit,
  Delete
} from '@element-plus/icons-vue'
import EditDialog from "@/views/article/components/EditDialog.vue";

const dialogRef = ref()
const loading = ref(false)
const list = ref([])
const getArticleList = async () => {
  loading.value = true
  const res = await getArticleCate()
  list.value = res.data.data
  loading.value = false
}
getArticleList()
//编辑
const edit = (row) => {
  dialogRef.value.open(row)
}
// 添加
const add = () => {
  dialogRef.value.open({})
}
const onSuccess = () => {
  getArticleList()
}
// 删除
const del = (id) => {
  ElMessageBox.confirm(
      '确认删除吗?',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        await delArticleCate(id)
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        getArticleList()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '删除失败',
        })
      })
}
</script>

<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="add">添加分类</el-button>
    </template>
    <el-table v-loading="loading" :data="list" style="width: 100%">
      <el-table-column type="index" label="序号" width="100"/>
      <el-table-column prop="cate_name" label="分类名称"/>
      <el-table-column prop="cate_alias" label="分类别名"/>
      <el-table-column label="操作" width="100">
        <template #default="{row,$index}">
          <el-button @click="edit(row)" size="small" type="primary" :icon="Edit" circle></el-button>
          <el-button @click="del(row.id)" size="small" type="danger" :icon="Delete" circle></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="暂无数据"/>
      </template>
    </el-table>
    <!--    弹层-->
    <EditDialog ref="dialogRef" @success="onSuccess"></EditDialog>
  </page-container>

</template>

<style lang="scss" scoped>

</style>

